﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using ESOL.Business.Sec;
using ESOL.Common;
using ESOL.Common.Entity;

namespace ESOL.WinUI.Sec
{
    public partial class frmMaintainUserRole : BaseForm.frmBaseDialogyMaintainForm
    {
        #region Variable
        public SecUserEntity UserEntity { get; set; }
        #endregion

        #region Constructor
        public frmMaintainUserRole()
        {
            InitializeComponent();
            ThisActionType = ActionType.GrandRole;
        }
        #endregion

        #region Functions
        private void BindTreeRole()
        {
            DataSet ds = SecRoleBs.GetDsAllRoles();
            treeRoles.DataSource = ds.Tables[0];
            treeRoles.ExpandAll();
        }

        private void SetFields()
        {
            if (UserEntity == null)
            {
                return;
            }
            txtUserAccount.Text = UserEntity.UserAccount;
            txtChineseName.Text = UserEntity.ChineseName;
            List<SecUserRoleEntity> list = SecUserRoleBs.GetSecUserRolesByUserID(UserEntity.UserId);
            SetTreeRoleChecked(list, treeRoles.Nodes);
        }

        private void SetTreeRoleChecked(List<SecUserRoleEntity> list, DevExpress.XtraTreeList.Nodes.TreeListNodes nodes)
        {
            if (list == null || nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
            {
                if (IsRoleInList(node.GetValue("ROLE_ID").ToString(), list))
                {
                    node.Checked = true;
                }
                else
                {
                    node.Checked = false;
                }
                SetTreeRoleChecked(list, node.Nodes);
            }
        }

        private bool IsRoleInList(string roleID, List<SecUserRoleEntity> list)
        {
            if (list == null)
            {
                return false;
            }
            foreach (SecUserRoleEntity entity in list)
            {
                if (entity.RoleId == roleID)
                {
                    return true;
                }
            }
            return false;
        }

        private bool SaveUserRole()
        {
            if (UserEntity == null)
            {
                return false;
            }
            try
            {
                List<SecUserRoleEntity> list = new List<SecUserRoleEntity>();
                GetUserRoleListFromTree(treeRoles.Nodes, list);
                bool isSuccessful = SecUserRoleBs.SaveUserRole(list, UserEntity.UserId);
                return isSuccessful;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return false;
            }
        }

        private void GetUserRoleListFromTree(DevExpress.XtraTreeList.Nodes.TreeListNodes nodes, List<SecUserRoleEntity> list)
        {
            if (UserEntity == null)
            {
                return;
            }
            if (list == null)
            {
                list = new List<SecUserRoleEntity>();
            }
            if (nodes == null || nodes.Count == 0)
            {
                return;
            }
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in nodes)
            {
                if (node.Checked == true)
                {
                    SecUserRoleEntity entity = new SecUserRoleEntity();
                    entity.UserId = UserEntity.UserId;
                    entity.RoleId = node.GetValue("ROLE_ID").ToString();
                    list.Add(entity);                    
                }
                GetUserRoleListFromTree(node.Nodes, list);
            }
        }
        #endregion

        #region Event
        private void frmMaintainUserRole_Load(object sender, EventArgs e)
        {
            BindTreeRole();
            SetFields();
        }

        protected override void Click_Save()
        {
            if (SaveUserRole())
            {
                this.DialogResult = DialogResult.OK;
            }
        }
        #endregion
    }
}
